package com.sunnypower.rushfitapp;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.sunnypower.rushfitapp.CourseHistorypart.VHistoryAdapter;
import com.sunnypower.rushfitapp.db.VHistory;
import com.sunnypower.rushfitapp.util.GetData;
import com.sunnypower.rushfitapp.util.ParseStringToOther;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class CourseHistoryActivity extends AppCompatActivity {
    private Button btn_back;
    private ListView history_course_list;
    private static final String TAG = "CourseHistoryActivity";
    private List<VHistory> historyList = new ArrayList<>();
    private String uid;
    private String hid;
    private List<VHistory> historyListTemp = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course_history);
        // 初始化控件
        history_course_list = findViewById(R.id.history_course_list);
        btn_back = findViewById(R.id.btn_back);
        uid = getIntent().getStringExtra("uid");
        // 设置返回键功能
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        getData();
    }

    private void getData() {
        // 请求服务器获取JSON数据
        String DataString = GetData.doGet("http://120.24.24.141:10990/rushfit/FindAllCourse?uid=" + uid);
        Log.d(TAG, "onCreate: " + DataString);
        // 解析JSON
        try {
            //获取JSONObject对象
            JSONObject jsonData = new JSONObject(DataString);
            JSONArray courseListJSON = jsonData.getJSONArray("history");
            for (int i = 0; i < courseListJSON.length(); i++) {
                JSONObject historyObject = courseListJSON.getJSONObject(i);
                VHistory vHistory = new VHistory();
                vHistory.setHid(historyObject.getInt("hid"));
                vHistory.setName(historyObject.getString("name"));
                vHistory.setTime(historyObject.getString("time"));
                if (!"null".equals(historyObject.getString("rate"))) {
                    vHistory.setRate(ParseStringToOther.strToFloat(historyObject.getString("rate")));
                } else {
                    vHistory.setRate(null);
                }
                historyList.add(vHistory);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        // 创建listView的自定义Adapter
        VHistoryAdapter adapter = new VHistoryAdapter(CourseHistoryActivity.this, R.layout.activity_v_history_part, historyListTemp);
        history_course_list.setAdapter(adapter);
        if (historyList.size() == 0) {
            history_course_list.setVisibility(View.GONE);
        } else {
            history_course_list.setVisibility(View.VISIBLE);
        }
        // 遍历列表，添加活动
        historyListTemp.clear();
        for (VHistory vHistory : historyList) {
            historyListTemp.add(vHistory);
        }
        historyList.clear();
        adapter.notifyDataSetChanged();
        history_course_list.setSelection(0);
        // 设置点击子物件时进行评价
        history_course_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                hid = historyListTemp.get(position).getHid().toString();
                Intent intent = new Intent(CourseHistoryActivity.this, RateCourseActivity.class);
                intent.putExtra("courseName", historyListTemp.get(position).getName());
                startActivityForResult(intent, 1);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:// 获取评价数据并保存到服务器
                if (resultCode == RESULT_OK) {
                    Float rate = Float.parseFloat(data.getStringExtra("rate"));
                    // 请求服务器设计评价数据
                    String DataString = GetData.doGet("http://120.24.24.141:10990/rushfit/rating?hid=" + hid + "&rate=" + rate);
                    Toast.makeText(getApplicationContext(), "评分成功！", Toast.LENGTH_SHORT).show();
//                    Intent intent = new Intent(CourseHistoryActivity.this, CourseHistoryActivity.class);
//                    intent.putExtra("uid", uid);
//                    startActivity(intent);
//                    finish();
                    getData();
                }
        }
    }
}
